package com.hpin.fansea.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.CommentList;
import com.hpin.fansea.bean.ResultBaseBean;
import com.hpin.fansea.utils.AbDateUtil;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.lidroid.xutils.http.RequestParams;

import java.util.Date;

/**
 * 评论页面
 * Created by Aaron on 15/9/9.
 */
public class ReleaseCommentActivity extends BaseActivity {

    //办事指南评论
    private static final int GUIDE_COMMENT = 1002;
//    //周边商家评论
//    private static final int SURROUND_COMMENT = 1003;

    private EditText et_comment;
    private String guiedId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_release_comment);

        guiedId = getIntent().getStringExtra("guiedId");

        initWidget();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.guide_work));

        iv_left.setOnClickListener(this);

        et_comment = (EditText) findViewById(R.id.et_comment);
        Button submit_comment = (Button) findViewById(R.id.submit_comment);

        submit_comment.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

            case R.id.submit_comment://发表评论

                releaseComment();

                break;


        }
    }


    protected void releaseComment() {

        // 验证评论内容
        if (TextUtils.isEmpty(et_comment.getText().toString().trim())) {
            showToast("评论内容不能为空");
            return;
        }

        // 发表评论
        CommentList json = new CommentList();
        json.commentCont = et_comment.getText().toString().trim();
        json.yltgId = guiedId;
        json.commentId = sp.getString(Constant.userId, "");

        String comment = JSON.toJSONString(json);

        /*
        Map<String, Object> params = new HashMap<String, Object>();
		params.put("token", Constant.token);
		params.put("comment", comment);
		params.put("systemType", SYSTEM_TYPE);
		String res = HttpUtil.httpPost(WEB_ROOT + AddGuideCommentList + "/"
				+ id, params);
         */
        RequestParams params=new RequestParams();
        params.addBodyParameter("token", sp.getString(Constant.token,""));
        params.addBodyParameter("comment", comment);
        params.addBodyParameter("systemType", Constant.SYSTEM_TYPE);

        HttpUtil.getInstance().setPostRequest("评论中...", this, Constant.WEB_ROOT + Constant.ADDGUIDECOMMENTLIST + "/" + guiedId,
                params, new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            ResultBaseBean resultBaseBean = JSONObject.parseObject(str, ResultBaseBean.class);

                            if (Constant.STATUS_SUCESS.equals(resultBaseBean.status)) {

                                showToast("评论成功");
                                /*
                                // 评论人nickname
        viewHolder.tv_tittle.setText(purchaseYlCommentLists.get(position).nickname);

        // 评论人头像
        ImageLoader.getInstance().displayImage(purchaseYlCommentLists.get(position).headPic, viewHolder.tv_img, options);

        // 评论内容
        viewHolder.tv_des.setText(purchaseYlCommentLists.get(position).commentCont);

        // 评论时间
         viewHolder.item_commemt_time.setText(purchaseYlCommentLists.get(position).commentTime);
                                 */

                                CommentList comment = new CommentList();
                                comment.nickname = sp.getString(Constant.nickname, "");
                                comment.headPic = sp.getString(Constant.headPic, "");
                                comment.commentCont = et_comment.getText().toString().trim();
                                comment.commentTime = AbDateUtil.getStringByFormat(new Date(), AbDateUtil.dateFormatYMD);

                                Intent data= new Intent();
                                data.putExtra("comment", comment);
                                setResult(GUIDE_COMMENT, data);
                                finish();

                            } else {
                                showToast(resultBaseBean.message);
                            }

                        } catch (Exception e) {

                            e.printStackTrace();
                            showToast("评论失败");
                        }

                    }
                }, null);

//        DataService.getAddGuideCommentList(mhandler, reviewJson, guiedId);

    }

}
